﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace HYSoft.Web
{
    public partial class sensor99_list : System.Web.UI.Page
    {
        public string typename;
        public string typename11;
        public int areano;
        protected void Page_Load(object sender, EventArgs e)
        {
            typename = Server.UrlDecode(Request.QueryString["type"].ToString());
            typename11 = Server.UrlEncode(Request.QueryString["type"].ToString());
            areano = int.Parse(Request.QueryString["areano"].ToString());
            data_list();
            data_show();
            data_tux();
            data_tux2();

        }

        protected void data_list()
        {
            int node_mac = int.Parse(Request.QueryString["mac"].ToString());
            string sql = " [mac]=" + node_mac + "";
            DataSet ds = new BLL.usr2node().GetList(sql);
            DataTable dt = ds.Tables[0];
            if (dt.Rows.Count > 0)
            {
                Repeater1.DataSource = dt;
                Repeater1.DataBind();
            }
            dt.Clear();
            dt.Dispose();
        }
        public DataTable gettable(object id)
        {
            string sql = " [index]=" + int.Parse(id.ToString()) + "";
            DataSet ds = new BLL.monitor().GetList(sql);
            DataTable dt = ds.Tables[0];
            return dt;
        }
        protected string data_type(object id)
        {
            string Status = "";
            DataTable dt = gettable(id);
            Status = dt.Rows[0]["Province_name"].ToString() + dt.Rows[0]["m_area_City"].ToString() + dt.Rows[0]["m_area_name"].ToString();
            dt.Clear();
            dt.Dispose();
            return Server.UrlEncode(Status);
        }

        protected string data_type111(object id)
        {
            string Status = "";
            DataTable dt = gettable(id);
            Status = dt.Rows[0]["Province_name"].ToString() + dt.Rows[0]["m_area_City"].ToString() + dt.Rows[0]["m_area_name"].ToString();
            dt.Clear();
            dt.Dispose();
            return Server.UrlDecode(Status);
        }

        protected string data_renyuan(object id)
        {
            string Status = "";
            DataTable dt = gettable(id);
            Status = dt.Rows[0]["contact_name1"].ToString();
            dt.Clear();
            dt.Dispose();
            return Status;
        }

        protected string data_phone(object id)
        {
            string Status = "";
            DataTable dt = gettable(id);
            Status = dt.Rows[0]["contact_mobile1"].ToString();
            dt.Clear();
            dt.Dispose();
            return Status;
        }
        public string tux;
        protected void data_tux()
        {
            int node_mac = int.Parse(Request.QueryString["mac"].ToString());
            string sql = " [mac]=" + int.Parse(node_mac.ToString()) + "";
            DataSet ds = new BLL.gprsNode().GetList(sql);
            DataTable dt = ds.Tables[0];
            tux = dt.Rows[0]["tux"].ToString();
            dt.Clear();
            dt.Dispose();
        }
        public string nowtux;
        protected void data_tux2()
        {
            int node_mac = int.Parse(Request.QueryString["mac"].ToString());
            string sql = " [mac]=" + int.Parse(node_mac.ToString()) + "";
            DataSet ds = new BLL.gprsNode().GetList(sql);
            DataTable dt = ds.Tables[0];
            nowtux = dt.Rows[0]["charvalue3"].ToString();
            dt.Clear();
            dt.Dispose();
        }
        protected string data_jibie(object id)
        {
            string name = "";
            int typeid = int.Parse(id.ToString());
            if (typeid == 0)
            {
                name = "<a style=\"color:green;\">正常</a>";
            }
            else
            {
                typeid = typeid & 0x80;
                if (typeid == 0x80)
                {
                    name = "<a style=\"color:red;\">报警</a>";
                }
                else
                {
                    name = "<a style=\"color:orange;\">预警</a>";
                }
            }
            return name;
        }
        protected string data_zhuangt(object id)
        {
            string zhuangt = "";
            string typeid = id.ToString();
            if (typeid == "正常")
            {
                zhuangt = "<a style=\"color:green;\">正常</a>";
            }
            else if (typeid == "预警")
            {
                zhuangt = "<a style=\"color:orange;\">预警</a>";
            }
            else
            {
                zhuangt = "<a style=\"color:red;\">" + typeid + "</a>";
            }
            return zhuangt;
        }
        protected string data_addrname(object id)
        {
            string addrname = "";
            DataTable dt = gettable(id);
            if (dt.Rows.Count > 0)
            {
                addrname = dt.Rows[0]["addr"].ToString();
            }

            return addrname;
        }
        protected void data_show()
        {
            int id = int.Parse(Request.QueryString["mac"].ToString());
            int curpage = Convert.ToInt32(this.labPage.Text);
            PagedDataSource ps = new PagedDataSource();
            string sql = " mac=" + id + " order by [index] desc";
            DataSet ds = new BLL.gprsNodeLog().GetList(sql);
            DataTable dt = ds.Tables[0];
            ps.DataSource = dt.DefaultView;
            ps.AllowPaging = true;
            ps.PageSize = 10;
            ps.CurrentPageIndex = curpage - 1;
            this.lnkbtnUp.Enabled = true;
            this.lnkbtnNext.Enabled = true;
            this.lnkbtnBack.Enabled = true;
            this.lnkbtnOne.Enabled = true;
            if (curpage == 1)
            {
                //不显示第一页按钮
                this.lnkbtnOne.Enabled = false;
                //不显示上一页按钮
                this.lnkbtnUp.Enabled = false;
            }
            if (curpage == ps.PageCount)
            {
                //不显示下一页
                this.lnkbtnNext.Enabled = false;
                //不显示最后一页
                this.lnkbtnBack.Enabled = false;
            }
            //显示分页数量
            this.labBackPage.Text = Convert.ToString(ps.PageCount);

            Repeater2.DataSource = ps;
            Repeater2.DataBind();
            dt.Clear();
            dt.Dispose();
        }
        protected void lnkbtnOne_Click(object sender, EventArgs e)
        {
            this.labPage.Text = "1";
            data_show();
        }
        protected void lnkbtnUp_Click(object sender, EventArgs e)
        {
            this.labPage.Text = Convert.ToString(Convert.ToInt32(this.labPage.Text) - 1);
            data_show();
        }
        protected void lnkbtnNext_Click(object sender, EventArgs e)
        {
            this.labPage.Text = Convert.ToString(Convert.ToInt32(this.labPage.Text) + 1);
            data_show();
        }
        protected void lnkbtnBack_Click(object sender, EventArgs e)
        {
            this.labPage.Text = this.labBackPage.Text;
            data_show();
        }
        protected void lbtnPaizhao_Click(object sender, EventArgs e)
        {
            int node_mac = int.Parse(Request.QueryString["mac"].ToString());
            try
            {
                string sql = " mac='" + node_mac + "'";
                DataSet ds = new BLL.command().GetList(sql);

                DataTable dt = ds.Tables[0];
                if (dt.Rows.Count < 1)
                {
                    Model.Zcommand command = new Model.Zcommand();
                    command.zcommand = 1;
                    command.mac = node_mac;
                    command.flag = 0;
                    new BLL.command().Add(command);
                    // da.insert_data("command", "command,mac,flag", "'1','" + node_mac + "','0'");

                }
                else
                {
                    string sql1 = " [mac]=" + int.Parse(node_mac.ToString()) + "";
                    DataSet ds1 = new BLL.command().GetList(sql);
                    Model.Zcommand zcommand = new BLL.command().GetModel(int.Parse(ds1.Tables[0].Rows[0]["index"].ToString()));
                    zcommand.zcommand = 1;
                    zcommand.flag = 0;
                    new BLL.command().Update(zcommand);
                    // da.updatatable("[command]", "[command]=1,[flag]=0  where [mac] = " + int.Parse(node_mac.ToString()) + "");
                }
                Response.Write("<script>alert('获取图像命令已发送！请等待五分钟再刷新网页查看新图像');window.location = 'sensor99_list.aspx?type=99&id=101&areano=32&mac=" + int.Parse(node_mac.ToString()) + "';</script>");

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}